<template>
    <a-form ref='formRef' :label-col='{span : 5}' :model='formData'>
        <a-form-item style='display:none' name='id'>
            <a-input type='hidden' v-model:value='formData.id' />
        </a-form-item>
        <a-form-item label='学号' name='xh' :rules="[{required : true, message : '请填写学号'}]">
            <a-input type='text' v-model:value='formData.gh' placeholder="请输入学号" />
        </a-form-item>
        <a-form-item label='姓名' name='name' :rules="[{required : true, message : '请填写姓名'}]">
            <a-input type='text' v-model:value='formData.name' placeholder="请输入姓名" />
        </a-form-item>
        <a-form-item label='性别' name='sex'>
            <a-radio-group v-model:value="formData.sex" name="sex">
                <a-radio :value="1">男</a-radio>
                <a-radio :value="2">女</a-radio>
            </a-radio-group>
        </a-form-item>
        <a-form-item label='手机号码' name='phone'>
            <a-input type='text' v-model:value='formData.phone' placeholder="请输入手机号码" />
        </a-form-item>
        <a-form-item label='学院' name='deptCode'>
            <a-tree-select v-model:value="formData.deptCode" placeholder="请选择部门" :tree-data="depts" style="width: 100%" :field-names="{ label: 'name', value: 'code', children: 'children'}"></a-tree-select>
        </a-form-item>
        <a-form-item label='年级' name='grade'>
            <a-input type='text' v-model:value='formData.grade' placeholder="请输入年级" />
        </a-form-item>
        <a-form-item label='班号' name='classNo'>
            <a-input type='text' v-model:value='formData.classNo' placeholder="请输入班号" />
        </a-form-item>
        
    </a-form>
</template>

<script lang="ts" setup>
    import { saveApi } from '@/api/Student';
    import { deptApi } from '@/api/SchoolDept';
import { isValidMobileNumber, recursionFn } from '@/utils';
import { Rule } from 'ant-design-vue/es/form';
    import { ref } from 'vue';


    const props = defineProps<{
        row? : any
    }>()


    const formRef = ref()
    const depts = ref<any[]>([])
    let bakData:any = {}

    
    const formData = ref({
        id : 0,
        xh : '',
        name : '',
        phone : '',
        deptCode : '',
        jobPost : '',
        position : '',
        sex : 1,
        grade:'',
        classNo:''

    })


    if(props.row) {
        bakData = {...props.row}
        for(let k in props.row) {
            if(props.row[k] !== null) {
                if(k in formData.value) {
                    if(k == 'classId') {
                        formData.value[k] = props.row[k] || ''
                    }else {
                        formData.value[k] = props.row[k]
                    }
                    
                }
            }
            
        }
    }


    async function submit() {
        let params  = await formRef.value.validate()
        if(bakData.id != undefined) { //编辑的
            if(bakData.phone == formData.value.phone) {
                delete params.phone;
            }

            if(params.password == '') {
                delete params.password;
            }
        }


        return saveApi(params)
    }

    defineExpose({
        submit
    })

    deptApi().then(r => {
        depts.value = recursionFn(r.data, 0, 'id', 'parentId', 'children')
    })
</script>